/*
 * Extended by:
 * src/edu/cmu/cs/stage3/alice/authoringtool/editors/compositeeditor/ForEachElementPanel.java
 * src/edu/cmu/cs/stage3/alice/authoringtool/editors/compositeeditor/IfElseElementPanel.java
 * src/edu/cmu/cs/stage3/alice/authoringtool/editors/compositeeditor/MainCompositeElementPanel.java
 * src/edu/cmu/cs/stage3/alice/authoringtool/editors/compositeeditor/WhileElementPanel.java
 * src/edu/cmu/cs/stage3/alice/authoringtool/editors/compositeeditor/LoopNElementPanel.java
 * src/edu/cmu/cs/stage3/alice/authoringtool/editors/responseeditor/CompositeResponsePanel.java
 * 
 */

package edu.cmu.cs.stage3.alice.authoringtool.editors.compositeeditor;

import edu.cmu.cs.stage3.resourceBundle.I18n;

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringTool;
import edu.cmu.cs.stage3.alice.authoringtool.AuthoringToolResources;
import edu.cmu.cs.stage3.alice.authoringtool.datatransfer.ElementReferenceTransferable;
import edu.cmu.cs.stage3.alice.authoringtool.editors.questioneditor.CompositeComponentQuestionPanel;
import edu.cmu.cs.stage3.alice.authoringtool.editors.questioneditor.MainCompositeQuestionPanel;
import edu.cmu.cs.stage3.alice.authoringtool.editors.responseeditor.CompositeComponentResponsePanel;
import edu.cmu.cs.stage3.alice.authoringtool.editors.responseeditor.ResponseEditor;
import edu.cmu.cs.stage3.util.StringObjectPair;

public abstract class CompositeElementPanel
  extends edu.cmu.cs.stage3.alice.authoringtool.util.DnDGroupingPanel
  implements CompositeComponentOwner, edu.cmu.cs.stage3.alice.authoringtool.util.GUIElement, edu.cmu.cs.stage3.alice.core.event.PropertyListener {

  protected CompositeComponentElementPanel componentElementPanel;
  protected edu.cmu.cs.stage3.alice.core.Element m_element;
  protected edu.cmu.cs.stage3.alice.core.property.ObjectArrayProperty m_components;
  protected String headerText = "Composite Element";
  protected javax.swing.JPanel headerPanel;
  protected javax.swing.JLabel closeBrace = new javax.swing.JLabel("}");
  protected javax.swing.JPanel containingPanel;
  protected javax.swing.JButton expandButton;
  protected javax.swing.JComponent nameInputField;
  protected javax.swing.Action expandAction;
  protected javax.swing.JLabel headerLabel;
  protected boolean isExpanded = true;
  protected java.awt.Color backgroundColor = new java.awt.Color(255,255,255);
  protected int m_depth = 0;
  protected javax.swing.ImageIcon plus;
  protected javax.swing.ImageIcon minus;
  protected java.awt.Component glue;
  protected AuthoringTool authoringTool;
  protected java.awt.event.ActionListener actionListener;
  //protected javax.swing.JPopupMenu popUpMenu;

  // Tentar chamar 'drop(final java.awt.dnd.DropTargetDropEvent dtde)' de 'src/edu/cmu/cs/stage3/alice/authoringtool/editors/responseeditor/CompositeComponentResponsePanel.java'
  public CompositeComponentElementPanel getCompositeComponentElementPanel () { return componentElementPanel; }

  protected java.awt.event.MouseListener elementMouseListener = new java.awt.event.MouseAdapter() {
    //Rem @Override
    public void mouseReleased (java.awt.event.MouseEvent ev) {
      if (ev.isPopupTrigger()) {
        if (CompositeElementPanel.this.getParent() != null) {
          java.util.Vector structure = edu.cmu.cs.stage3.alice.authoringtool.util.ElementPopupUtilities.getDefaultStructure( CompositeElementPanel.this.m_element );
          if ( !(ForEachElementPanel.class.isAssignableFrom(CompositeElementPanel.this.getClass())) && !(LoopNElementPanel.class.isAssignableFrom(CompositeElementPanel.this.getClass())) ) {
            Runnable dissolveRunnable = new Runnable() {
              public void run() {
                dissolve();
                }
              };
            StringObjectPair dissolveEntry = new StringObjectPair("dissolve", dissolveRunnable);
            if (structure != null) {
              structure.add(dissolveEntry);
              }
            }
          if (structure != null) {
            edu.cmu.cs.stage3.alice.authoringtool.util.ElementPopupUtilities.createAndShowElementPopupMenu( CompositeElementPanel.this.m_element, structure, CompositeElementPanel.this, ev.getX(), ev.getY() );
            }
        }
      }
    }
  };

  protected static String IS_EXPANDED_KEY = "edu.cmu.cs.stage3.alice.authoringtool.editors.compositeeditor IS_EXPANDED_KEY";

  public CompositeElementPanel () {
    System.out.println("CompositeElementPanel.java: CompositeElementPanel()");
    //T try {String str=""; System.out.println(str.charAt(3));}catch(Exception e) {e.printStackTrace();}
    //T edu.cmu.cs.stage3.util.StrUtilities.findMainContainer(this,true);

    headerText = "Element Response";
    actionListener = new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent e) {
        if (isExpanded) {
          reduceComponentElementPanel();
          }
        else {
          expandComponentElementPanel();
          }
        }
      };
    backgroundColor = getCustomBackgroundColor();
    generateGUI();
    }

  public void set (edu.cmu.cs.stage3.alice.core.Element element, AuthoringTool authoringToolIn) {
    clean();
    super.reset();
    authoringTool = authoringToolIn;
    m_element = element;
    variableInit();
    startListening();
    setHeaderLabel();
    updateGUI();
    setDropTargets();
    }

  protected java.awt.Color getCustomBackgroundColor () {
    return backgroundColor;
    }

  protected void setDropTargets () {
    headerLabel.setDropTarget(new java.awt.dnd.DropTarget( headerLabel, componentElementPanel));
    this.setDropTarget(new java.awt.dnd.DropTarget( this, componentElementPanel));
    containingPanel.setDropTarget(new java.awt.dnd.DropTarget( containingPanel, componentElementPanel));
    headerPanel.setDropTarget(new java.awt.dnd.DropTarget( headerPanel, componentElementPanel));
    grip.setDropTarget(new java.awt.dnd.DropTarget( grip, componentElementPanel));
    glue.setDropTarget(new java.awt.dnd.DropTarget( glue, componentElementPanel));
    expandButton.setDropTarget(new java.awt.dnd.DropTarget( expandButton, componentElementPanel));
    }

  protected void variableInit () {
    Object isExpandedValue = m_element.data.get( IS_EXPANDED_KEY );
    backgroundColor = getCustomBackgroundColor(); // ? 'getCustomBackgroundColor()' faz 'return backgroundColor'
    if (isExpandedValue instanceof Boolean) {
      isExpanded = ((Boolean)isExpandedValue).booleanValue();
      }
    setTransferable(new ElementReferenceTransferable(m_element));
    if (m_element instanceof edu.cmu.cs.stage3.alice.core.response.CompositeResponse) {
      edu.cmu.cs.stage3.alice.core.response.CompositeResponse proxy = (edu.cmu.cs.stage3.alice.core.response.CompositeResponse)m_element;
      m_components = proxy.componentResponses;

      // protected CompositeComponentElementPanel componentElementPanel;
      componentElementPanel = new CompositeComponentResponsePanel();

      componentElementPanel.set(m_components, this, authoringTool);
      componentElementPanel.setBackground(backgroundColor);
      this.addDragSourceComponent(componentElementPanel);
      componentElementPanel.addMouseListener(elementMouseListener);
      }
    else if (m_element instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.Composite) {
      edu.cmu.cs.stage3.alice.core.question.userdefined.Composite proxy = (edu.cmu.cs.stage3.alice.core.question.userdefined.Composite)m_element;
      m_components = proxy.components;
      componentElementPanel = new CompositeComponentQuestionPanel();
      componentElementPanel.set(m_components, this, authoringTool);
      componentElementPanel.setBackground(backgroundColor);
      this.addDragSourceComponent(componentElementPanel);
      componentElementPanel.addMouseListener(elementMouseListener);
      }
    }

  public java.awt.Component getGrip() {
    return this.grip;
    }

  public java.awt.Container getParent() {
    return super.getParent();
    }

  protected void startListening() {
    if (m_element != null) {
      m_element.name.addPropertyListener(this);
      }
    }

  protected void stopListening() {
    if (m_element != null) {
      m_element.name.removePropertyListener(this);
      }
    }

  public void goToSleep () {
    stopListening();
    if (componentElementPanel != null) {
      componentElementPanel.goToSleep();
      }
    }

  public void wakeUp () {
    startListening();
    if (componentElementPanel != null) {
      componentElementPanel.wakeUp();
      }
    }

  public void release () {
    super.release();
    if (componentElementPanel != null) {
      componentElementPanel.release();
      }
    edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.releaseGUI( this );
    }

  public void clean () {
    goToSleep();
    if (componentElementPanel != null) {
      if (containingPanel != null) {
        containingPanel.remove( componentElementPanel );
        }
      componentElementPanel.release();
      componentElementPanel = null;
      }
    }

  protected void removeAllListening () {
System.out.println("\n\n\nCompositeElementPanel.java: removeAllListening()");
    this.removeDragSourceComponent(componentElementPanel);
    componentElementPanel.removeMouseListener(elementMouseListener);
    grip.setDropTarget(null);
    this.removeDragSourceComponent(glue);
    glue.removeMouseListener(elementMouseListener);
    this.removeMouseListener(elementMouseListener);
    grip.removeMouseListener(elementMouseListener);
    expandButton.removeActionListener(actionListener);
    this.setTransferable(null);
    if (headerLabel != null) {
      headerLabel.removeMouseListener(elementMouseListener);
      this.removeDragSourceComponent(headerLabel);
      }
    if (containingPanel != null) {
      containingPanel.removeMouseListener(elementMouseListener);
      this.removeDragSourceComponent(containingPanel);
      }
    if (headerPanel != null) {
      headerPanel.removeMouseListener(elementMouseListener);
      this.removeDragSourceComponent(headerPanel);
      }
    }

  public void die () {
    clean();
    removeAllListening();
    }

  public void dissolve () {
    if (m_element.getParent() instanceof edu.cmu.cs.stage3.alice.core.response.CompositeResponse) {
      authoringTool.getUndoRedoStack().startCompound();
      edu.cmu.cs.stage3.alice.core.response.CompositeResponse parent = (edu.cmu.cs.stage3.alice.core.response.CompositeResponse)m_element.getParent();
      int index = parent.componentResponses.indexOf(m_element);
      if (parent instanceof edu.cmu.cs.stage3.alice.core.response.IfElseInOrder) {
        if (!parent.componentResponses.contains(m_element)) {
          index = ((edu.cmu.cs.stage3.alice.core.response.IfElseInOrder)parent).elseComponentResponses.indexOf(m_element);
          }
        }
      Object responses[] = m_components.getArrayValue();
      for (int i=0; i<responses.length; i++) {
        if (responses[i] instanceof edu.cmu.cs.stage3.alice.core.Response) {
          edu.cmu.cs.stage3.alice.core.Response currentResponse = (edu.cmu.cs.stage3.alice.core.Response)responses[i];
          currentResponse.removeFromParent();
          currentResponse.setParent(parent);
          parent.componentResponses.add(index+i, currentResponse);
          }
        }
      m_element.removeFromParent();
      authoringTool.getUndoRedoStack().stopCompound();
      }
    else if (m_element.getParent() instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.Composite) {

      authoringTool.getUndoRedoStack().startCompound();
      edu.cmu.cs.stage3.alice.core.question.userdefined.Composite parent = (edu.cmu.cs.stage3.alice.core.question.userdefined.Composite)m_element.getParent();
      int index = parent.getIndexOfChild(m_element);
      if (parent instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.IfElse) {
        if (!parent.components.contains(m_element)) {
          index = ((edu.cmu.cs.stage3.alice.core.question.userdefined.IfElse)parent).elseComponents.indexOf(m_element);
          }
        }

      Object questions[] = m_components.getArrayValue();
      for (int i=0; i<questions.length; i++) {
        if (questions[i] instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.Component) {
          edu.cmu.cs.stage3.alice.core.question.userdefined.Component currentQuestion = (edu.cmu.cs.stage3.alice.core.question.userdefined.Component)questions[i];
          currentQuestion.removeFromParent();
          currentQuestion.setParent(parent);
          parent.components.add(index+i, currentQuestion);
          }
        }
      m_element.removeFromParent();
      authoringTool.getUndoRedoStack().stopCompound();
      }
    else if (m_element.getParent() instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.UserDefinedQuestion) {
      authoringTool.getUndoRedoStack().startCompound();
      edu.cmu.cs.stage3.alice.core.question.userdefined.UserDefinedQuestion parent = (edu.cmu.cs.stage3.alice.core.question.userdefined.UserDefinedQuestion)m_element.getParent();
      int index = parent.getIndexOfChild(m_element);

      Object questions[] = m_components.getArrayValue();
      for (int i=0; i<questions.length; i++) {
        if (questions[i] instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.Component) {
          edu.cmu.cs.stage3.alice.core.question.userdefined.Component currentQuestion = (edu.cmu.cs.stage3.alice.core.question.userdefined.Component)questions[i];
          currentQuestion.removeFromParent();
          currentQuestion.setParent(parent);
          parent.components.add(index+i, currentQuestion);
          }
        }
      m_element.removeFromParent();
      authoringTool.getUndoRedoStack().stopCompound();
      }
    }

  public void propertyChanging (edu.cmu.cs.stage3.alice.core.event.PropertyEvent propertyEvent) { }

  public void propertyChanged (edu.cmu.cs.stage3.alice.core.event.PropertyEvent propertyEvent) {
    updateGUI();
    }

  public void setBackground (java.awt.Color color) {
    super.setBackground(color);
    backgroundColor = color;
    if (containingPanel != null) {containingPanel.setBackground(backgroundColor);}
    if (headerLabel != null) {headerLabel.setBackground(backgroundColor);}
    if (headerPanel != null) {headerPanel.setBackground(backgroundColor);}
    if (componentElementPanel != null) {componentElementPanel.setBackground(backgroundColor);}
    }

  protected void generateGUI () {
    this.setOpaque(false);
    plus = new javax.swing.ImageIcon( CompositeElementPanel.class.getResource( "images/plus.gif" ) );
    minus = new javax.swing.ImageIcon( CompositeElementPanel.class.getResource( "images/minus.gif" ) );
    expandButton = new javax.swing.JButton();
    expandButton.setContentAreaFilled( false );
    expandButton.setMargin( new java.awt.Insets( 0, 0, 0, 0 ) );
    expandButton.setFocusPainted( false );
    expandButton.setBorderPainted( false );
    expandButton.setBorder(null);
    expandButton.addActionListener(actionListener);
    glue = javax.swing.Box.createHorizontalGlue();
    this.addDragSourceComponent(glue);
    glue.addMouseListener(elementMouseListener);
    this.addMouseListener(elementMouseListener);
    grip.addMouseListener(elementMouseListener);
    if (headerLabel == null) {
      headerLabel = new javax.swing.JLabel();
      setHeaderLabel();
      headerLabel.setOpaque(false);
      headerLabel.addMouseListener(elementMouseListener);
      this.addDragSourceComponent(headerLabel);
      }
    if (containingPanel == null) {
      containingPanel = new javax.swing.JPanel();
      containingPanel.setLayout(new java.awt.BorderLayout());
      containingPanel.addMouseListener(elementMouseListener);
      containingPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,0,0,0));
      containingPanel.setOpaque(false);
      this.addDragSourceComponent(containingPanel);
      }
    if (headerPanel == null) {
      headerPanel = new javax.swing.JPanel();
      headerPanel.setLayout(new java.awt.GridBagLayout());
      headerPanel.addMouseListener(elementMouseListener);
      headerPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,0,0,0));
      headerPanel.setOpaque(false);
      this.addDragSourceComponent(headerPanel);
      }
    }

  protected void restoreDrag () {
    this.addDragSourceComponent(glue);
    this.addDragSourceComponent(headerPanel);
    this.addDragSourceComponent(containingPanel);
    this.addDragSourceComponent(headerLabel);
    }

  protected void updateGUI () {
    containingPanel.removeAll();
    headerPanel.removeAll();
    restoreDrag();
    if (isExpanded) {
      expandButton.setIcon(minus);
      }
    else {
      expandButton.setIcon(plus);
      }
    headerPanel.add(expandButton, new java.awt.GridBagConstraints(0,0,1,1,0,0,java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.NONE, new java.awt.Insets(0,0,0,0), 0,0));
    headerPanel.add(headerLabel, new java.awt.GridBagConstraints(1,0,1,1,0,0,java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.NONE, new java.awt.Insets(0,2,0,2), 0,0));
    headerPanel.add(glue, new java.awt.GridBagConstraints(3,0,1,1,1,0,java.awt.GridBagConstraints.CENTER, java.awt.GridBagConstraints.NONE, new java.awt.Insets(0,0,0,0), 0,0));
    containingPanel.add(headerPanel, java.awt.BorderLayout.NORTH);
    if (isExpanded) {
      containingPanel.add(componentElementPanel, java.awt.BorderLayout.CENTER);
      if (ResponseEditor.IS_JAVA) {
        containingPanel.add(closeBrace, java.awt.BorderLayout.SOUTH);
        }
      }
    this.add(containingPanel,  java.awt.BorderLayout.CENTER);
    this.setBackground(getCustomBackgroundColor());
    }

  public boolean isExpanded () {
    if (isExpanded) {
      return true;
      }
    return false;
    }

  public void expandComponentElementPanel () {
    if (!isExpanded) {
      m_element.data.put( IS_EXPANDED_KEY, Boolean.TRUE );
      isExpanded = true;
      setHeaderLabel();
      expandButton.setIcon(minus);
      /*  if (componentElementPanel.getComponentCount() == 0) {
              componentElementPanel = new CompositeComponentElementPanel();
              componentElementPanel.set(m_components, this, authoringTool);
      }*/
      containingPanel.add(componentElementPanel, java.awt.BorderLayout.CENTER);
      if (ResponseEditor.IS_JAVA) {
        containingPanel.add(closeBrace, java.awt.BorderLayout.SOUTH);
        }
      this.revalidate();
      // this.repaint();
      }
    }

  public void setHeaderLabel () {
    if (headerLabel != null) {
      headerLabel.setText(headerText);
      if (ResponseEditor.IS_JAVA) {
        if (!isExpanded) {
          headerLabel.setText(headerText+" { "+getDots()+" }");
          }
        else {
          headerLabel.setText(headerText+" {");
          }
        }
      }
    }


  protected static int getEmptyRows (Object[] elements) {
    int count = 0;
    //LOB for (Object element : elements)
    int t_tam = elements!=null ? elements.length : 0;
    for (int i_i=0; i_i<t_tam; i_i++) {
      if (elements[i_i] instanceof edu.cmu.cs.stage3.alice.core.response.CompositeResponse) { //L if (element instanceof ...)
        if (((edu.cmu.cs.stage3.alice.core.response.CompositeResponse)elements[i_i]).componentResponses.size() == 0) { //L (...)element).componentResponses.size() == 0)
          count++;
          }
        if (elements[i_i] instanceof edu.cmu.cs.stage3.alice.core.response.IfElseInOrder) { //L if (element instanceof ...)
          if (((edu.cmu.cs.stage3.alice.core.response.IfElseInOrder)elements[i_i]).elseComponentResponses.size() == 0) { //L (...)element).elseComponentResponses.size() == 0)
            count++;
            }
          }
        }
      else
      if (elements[i_i] instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.Composite) { //L if (element instanceof...)
        if (((edu.cmu.cs.stage3.alice.core.question.userdefined.Composite)elements[i_i]).components.size() == 0) { //L (...)element).components.size() == 0)
          count++;
          }
        if (elements[i_i] instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.IfElse) { //L if (element instanceof...)
          if (((edu.cmu.cs.stage3.alice.core.question.userdefined.IfElse)elements[i_i]).elseComponents.size() == 0) { //L (...)element).elseComponents.size() == 0)
            count++;
            }
        }
      } // if (elements[i_i] instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.Composite)
    } // for (int i_i=0; i_i<t_tam; i_i++)
    return count;
    }

  protected static int getTotalRowsToRenderForIfElse (edu.cmu.cs.stage3.alice.core.Element ifElse, edu.cmu.cs.stage3.alice.core.property.ObjectArrayProperty components) {
    int total = 0;
    java.util.Vector potentiallyEmpty = new java.util.Vector();
    if (ifElse instanceof edu.cmu.cs.stage3.alice.core.response.IfElseInOrder) {
      for (int i=0; i<components.size(); i++) {
        if (components.get(i) instanceof edu.cmu.cs.stage3.alice.core.response.CompositeResponse) {
          edu.cmu.cs.stage3.alice.core.Element[] ds = ((edu.cmu.cs.stage3.alice.core.Element)components.get(i)).getDescendants(edu.cmu.cs.stage3.alice.core.Response.class);
          total += ds.length;
          total += ((edu.cmu.cs.stage3.alice.core.Element)components.get(i)).getDescendants(edu.cmu.cs.stage3.alice.core.response.IfElseInOrder.class).length;
          potentiallyEmpty.addAll(java.util.Arrays.asList(((edu.cmu.cs.stage3.alice.core.Element)components.get(i)).getDescendants(edu.cmu.cs.stage3.alice.core.response.CompositeResponse.class)));
          if (CompositeElementEditor.IS_JAVA) {
            total += ((edu.cmu.cs.stage3.alice.core.Element)components.get(i)).getDescendants(edu.cmu.cs.stage3.alice.core.response.CompositeResponse.class).length; // add on the extra "}" for each composite
            }
        } else {
          total++;
          }
        }
    } else
    if (ifElse instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.IfElse) {
      for (int i=0; i<components.size(); i++) {
        total++;
        if (components.get(i) instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.IfElse) {
          total++;
          }
        if (components.get(i) instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.Composite) {
          potentiallyEmpty.add(components.get(i));
          }
        total += ((edu.cmu.cs.stage3.alice.core.Element)components.get(i)).getDescendants(edu.cmu.cs.stage3.alice.core.question.userdefined.Component.class).length;
        total += ((edu.cmu.cs.stage3.alice.core.Element)components.get(i)).getDescendants(edu.cmu.cs.stage3.alice.core.question.userdefined.IfElse.class).length;
        potentiallyEmpty.addAll(java.util.Arrays.asList(((edu.cmu.cs.stage3.alice.core.Element)components.get(i)).getDescendants(edu.cmu.cs.stage3.alice.core.question.userdefined.Composite.class)));
        if (CompositeElementEditor.IS_JAVA) {
          total += ((edu.cmu.cs.stage3.alice.core.Element)components.get(i)).getDescendants(edu.cmu.cs.stage3.alice.core.question.userdefined.Composite.class).length; // add on the extra "}" for each composite
          }
        }
      } // if (ifElse instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.IfElse)
    int emptyRows = getEmptyRows(potentiallyEmpty.toArray());
    total += emptyRows;
    return total;
    }

  protected String getHTMLColorString (java.awt.Color color) {
    String r = Integer.toHexString(color.getRed());
    String g = Integer.toHexString(color.getGreen());
    String b = Integer.toHexString(color.getBlue());

    if (r.length() == 1) {
      r = "0"+r;
      }
    if (g.length() == 1) {
      g = "0"+g;
      }
    if (b.length() == 1) {
      b = "0"+b;
      }
    return new String("#"+r+g+b);
    }

  protected String getHeaderHTML () {
    return edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getHTMLStringForComponent(headerPanel);
    }

  public static int getTotalHTMLRows (edu.cmu.cs.stage3.alice.core.Element element) {
    int totalRows = 0;
    if (element instanceof edu.cmu.cs.stage3.alice.core.Response) {
      if (element instanceof edu.cmu.cs.stage3.alice.core.response.IfElseInOrder) {
        totalRows = getTotalRowsToRenderForIfElse(element, ((edu.cmu.cs.stage3.alice.core.response.IfElseInOrder)element).componentResponses );
      } else {
        totalRows = (element.getDescendants(edu.cmu.cs.stage3.alice.core.Response.class).length - 1);
        totalRows += element.getDescendants(edu.cmu.cs.stage3.alice.core.response.IfElseInOrder.class).length;
        totalRows += getEmptyRows(element.getDescendants(edu.cmu.cs.stage3.alice.core.response.CompositeResponse.class));

        if (CompositeElementEditor.IS_JAVA) {
          totalRows += (element.getDescendants(edu.cmu.cs.stage3.alice.core.response.CompositeResponse.class).length - 1); // add on the extra "}" for each composite
          }
        }
    } else if (element instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.Composite || (element instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.UserDefinedQuestion)) {
      if (element instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.IfElse) {
        totalRows = getTotalRowsToRenderForIfElse(element, ((edu.cmu.cs.stage3.alice.core.question.userdefined.IfElse)element).components );
      } else {
        totalRows = (element.getDescendants(edu.cmu.cs.stage3.alice.core.question.userdefined.Component.class).length -1);
        totalRows += element.getDescendants(edu.cmu.cs.stage3.alice.core.question.userdefined.IfElse.class).length;
        totalRows += getEmptyRows(element.getDescendants(edu.cmu.cs.stage3.alice.core.question.userdefined.Composite.class));
        if (CompositeElementEditor.IS_JAVA) {
          totalRows += (element.getDescendants(edu.cmu.cs.stage3.alice.core.question.userdefined.Composite.class).length - 1); // add on the extra "}" for each composite
          }
        }
      }
    return totalRows;
    }

  public void getHTML (StringBuffer toWriteTo, int colSpan, boolean useColor, boolean isDisabled) {
    int totalRows = 0;
    String colorString = "";
    String borderColorString = getHTMLColorString(java.awt.Color.lightGray);
    String styleString = "";
    String strikeStart = "";
    String strikeEnd = "";
    if (!isDisabled) {
      isDisabled = this.isDisabled();
      }
    if (useColor) {
      if (isDisabled) {
        colorString = " bgcolor=\""+getHTMLColorString(AuthoringToolResources.getColor("disabledHTML"))+"\"";
      } else {
        colorString = " bgcolor=\""+getHTMLColorString(getCustomBackgroundColor())+"\"";
        }
      }
    if (isDisabled) {
      strikeStart = "<strike><font color=\""+getHTMLColorString(AuthoringToolResources.getColor("disabledHTMLText"))+"\">";
      strikeEnd = "</font></strike>";
      }
    totalRows = getTotalHTMLRows(this.m_element);
    if (this instanceof  MainCompositeQuestionPanel) {
       totalRows++;
       }
    styleString = " style=\"border-left: 1px solid "+borderColorString+"; border-top: 1px solid "+borderColorString+"; border-right: 1px solid "+borderColorString+"\"";
    toWriteTo.append("<td colspan=\""+colSpan+"\""+colorString+styleString+">&nbsp;&nbsp;");

    toWriteTo.append(strikeStart+getHeaderHTML()+strikeEnd);
    if (this instanceof IfElseElementPanel || CompositeElementEditor.IS_JAVA) {
      styleString = " style=\"border-left: 1px solid "+borderColorString+"\"";
    } else {
      styleString = " style=\"border-left: 1px solid "+borderColorString+"; border-bottom: 1px solid "+borderColorString+"\"";
      }
    toWriteTo.append("</td>\n</tr>\n<tr>\n<td width=\"20\" rowspan=\""+(totalRows)+"\""+colorString+styleString+">&nbsp;&nbsp;&nbsp;&nbsp;</td>\n");
    for (int i=0; i<componentElementPanel.getElementComponentCount(); i++) {
      if (i>0) {
        toWriteTo.append("<tr>\n");
        }
      java.awt.Component currentComponent = componentElementPanel.getComponent(i);
      if (currentComponent instanceof CompositeElementPanel) {
        ((CompositeElementPanel)currentComponent).getHTML(toWriteTo, colSpan-1, useColor, isDisabled);
      } else {
        String componentColorString = "";
        String componentStrikeStart = "";
        String componentStrikeEnd = "";
        boolean isComponentDisabled = (isDisabled || ((ComponentElementPanel)currentComponent).isDisabled());
        if (currentComponent instanceof ComponentElementPanel) {
          if (isComponentDisabled) {
            componentColorString = " bgcolor=\""+getHTMLColorString(AuthoringToolResources.getColor("disabledHTML"))+"\"";
            componentStrikeStart = "<strike><font color=\""+getHTMLColorString(AuthoringToolResources.getColor("disabledHTMLText"))+"\">";
            componentStrikeEnd = "</font></strike>";
          } else {
            componentColorString = " bgcolor=\""+getHTMLColorString(((ComponentElementPanel)currentComponent).getCustomBackgroundColor())+"\"";
            }
          }
        styleString = " style=\"border-bottom: 1px solid "+borderColorString+"; border-right: 1px solid "+borderColorString+"; border-left: 1px solid "+borderColorString+"; border-top: 1px solid "+borderColorString+"\"";
        toWriteTo.append("<td width=\"100%\" colspan=\""+(colSpan-1)+"\""+componentColorString+styleString+">&nbsp;&nbsp;"+componentStrikeStart+edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getHTMLStringForComponent(currentComponent)+componentStrikeEnd+"</td>\n");
        // System.out.println("CompositeElementPanel.java: "+toWriteTo);
        }

      }
    if (componentElementPanel.getElementComponentCount() == 0) {
      if (this instanceof IfElseElementPanel || CompositeElementEditor.IS_JAVA) {
        styleString = " style=\"border-right: 1 solid "+borderColorString+"\"";
      } else {
        styleString = " style=\"border-right: 1 solid "+borderColorString+"; border-bottom: 1 solid "+borderColorString+"\"";
        }
      toWriteTo.append("<td width=\"100%\" colspan=\""+(colSpan-1)+"\""+colorString+styleString+">"+strikeStart+"<i>"+I18n.getString("doNothing")+"</i>"+strikeEnd+"</td>\n</tr>\n");
      }
    if (this instanceof MainCompositeQuestionPanel) {
      MainCompositeQuestionPanel mainQuestion = (MainCompositeQuestionPanel)this;
      styleString = " style=\"border-bottom: 1 solid "+borderColorString+"; border-right: 1 solid "+borderColorString+"; border-left: 1 solid "+borderColorString+";\"";
      colorString = " bgcolor=\""+getHTMLColorString(AuthoringToolResources.getColor("Return"))+"\"";
      toWriteTo.append("<tr>\n<td width=\"100%\" colspan=\""+(colSpan)+"\""+colorString+styleString+">"+
          edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getHTMLStringForComponent(mainQuestion.returnPanel)+
      "</td>\n</tr>\n");
      }
    if (this instanceof IfElseElementPanel) {
      IfElseElementPanel ifElse = (IfElseElementPanel)this;
      if (this.m_element instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.IfElse) {
        totalRows = getTotalRowsToRenderForIfElse(this.m_element, ((edu.cmu.cs.stage3.alice.core.question.userdefined.IfElse)this.m_element).elseComponents );
      } else if (this.m_element instanceof edu.cmu.cs.stage3.alice.core.response.IfElseInOrder) {
        totalRows = getTotalRowsToRenderForIfElse(this.m_element, ((edu.cmu.cs.stage3.alice.core.response.IfElseInOrder)this.m_element).elseComponentResponses );
        }
      styleString = " style=\"border-left: 1 solid "+borderColorString+"; border-right: 1 solid "+borderColorString+"\"";
      toWriteTo.append("<tr>\n<td colspan=\""+colSpan+"\""+colorString+styleString+">&nbsp;&nbsp;"+strikeStart+edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getHTMLStringForComponent(((IfElseElementPanel)this).elsePanel)+ strikeEnd+"</td>\n");
      if (CompositeElementEditor.IS_JAVA) {
        styleString = " style=\"border-left: 1 solid "+borderColorString+"\"";
      } else {
        styleString = " style=\"border-left: 1 solid "+borderColorString+"; border-bottom: 1 solid "+borderColorString+"\"";
        }
      toWriteTo.append("</tr>\n<tr>\n<td width=\"20\" rowspan=\""+(totalRows)+"\""+colorString+styleString+">&nbsp;</td>\n");
      for (int i=0; i<ifElse.elseComponentPanel.getElementComponentCount(); i++) {
        if (i>0) {
          toWriteTo.append("<tr>\n");
          }
        java.awt.Component currentComponent = ifElse.elseComponentPanel.getComponent(i);
        if (currentComponent instanceof CompositeElementPanel) {
          ((CompositeElementPanel)currentComponent).getHTML(toWriteTo, colSpan-1, useColor, isDisabled);
        } else {
          String componentColorString = "";
          String componentStrikeStart = "";
          String componentStrikeEnd = "";
          boolean isComponentDisabled = (isDisabled || ((ComponentElementPanel)currentComponent).isDisabled());
          if (currentComponent instanceof ComponentElementPanel) {
            if (isComponentDisabled) {
              componentColorString = " bgcolor=\""+getHTMLColorString(AuthoringToolResources.getColor("disabledHTML"))+"\"";
              componentStrikeStart = "<strike><font color=\""+getHTMLColorString(AuthoringToolResources.getColor("disabledHTMLText"))+"\">";
              componentStrikeEnd = "</font></strike>";
            } else {
              componentColorString = " bgcolor=\""+getHTMLColorString(((ComponentElementPanel)currentComponent).getCustomBackgroundColor())+"\"";
              }
            }
          styleString = " style=\"border-bottom: 1 solid "+borderColorString+"; border-right: 1 solid "+borderColorString+"; border-left: 1 solid "+borderColorString+"; border-top: 1 solid "+borderColorString+"\"";
          toWriteTo.append("<td colspan=\""+(colSpan-1)+"\""+componentColorString+styleString+">&nbsp;&nbsp;"+componentStrikeStart+edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getHTMLStringForComponent(currentComponent)+componentStrikeEnd+"</td>\n");
          }
        toWriteTo.append("</tr>\n");
        }
      if (ifElse.elseComponentPanel.getElementComponentCount() == 0) {
        if (!CompositeElementEditor.IS_JAVA) {
          styleString = " style=\"border-right: 1 solid "+borderColorString+"; border-bottom: 1 solid "+borderColorString+"\"";
        } else {
          styleString = " style=\"border-right: 1 solid "+borderColorString+"\"";
          }
	// doNothing=Do Nothing
        toWriteTo.append("<td colspan=\""+(colSpan-1)+"\""+colorString+styleString+">"+strikeStart+"<i>"+I18n.getString("doNothing")+"</i>"+strikeEnd+"</td>\n</tr>\n");
        }
      }
    if (CompositeElementEditor.IS_JAVA) {
      styleString = " style=\"border-left: 1 solid "+borderColorString+"; border-bottom: 1 solid "+borderColorString+"; border-right: 1 solid "+borderColorString+"\"";
      toWriteTo.append("<tr><td colspan=\""+colSpan+"\""+colorString+styleString+">"+strikeStart+"<b>&nbsp;&nbsp;}</b>"+strikeEnd+"</td></tr>");
      }

    }

  public String getDots () {
    String dots = "";
    for (int i=0; i<m_components.size(); i++) {
      if (i == 0) {
        dots += ".";
        }
      else {
        dots += " .";
        }
      }
    return dots;
    }

  public void reduceComponentElementPanel () {
    if (isExpanded) {
      m_element.data.put( IS_EXPANDED_KEY, Boolean.FALSE );
      isExpanded = false;
      setHeaderLabel();
      expandButton.setIcon(plus);
      containingPanel.remove(componentElementPanel);
      if (ResponseEditor.IS_JAVA) {
        containingPanel.remove(closeBrace);
        }
      this.revalidate();
      //   this.repaint();
      }
    }

  public CompositeComponentElementPanel getComponentPanel () {
    return componentElementPanel;
    }

  public edu.cmu.cs.stage3.alice.core.Element getElement () {
    return m_element;
    }

  public boolean isDisabled () {
    boolean isEnabledValue = true;
    if (m_element instanceof edu.cmu.cs.stage3.alice.core.Response) {
      isEnabledValue = true;
      }
    else if (m_element instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.Component) {
      isEnabledValue = true;
      }
    return !isEnabledValue;
    }

  public void paintForeground (java.awt.Graphics g) {
    super.paintForeground( g );
    if (m_element != null) {
      if (isDisabled()) {
        java.awt.Rectangle bounds = new java.awt.Rectangle(0,0,this.getWidth(), this.getHeight());
        edu.cmu.cs.stage3.alice.authoringtool.util.GUIEffects.paintDisabledEffect( g, bounds );
        }
      }
    }

  }
